/*
 * Copyright (C) 2015 Square, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.one.common.model.http.converter;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Converter;

final class MyGsonResponseBodyConverter<T> implements Converter<ResponseBody, Object> {
    private final Gson gson;
    private final TypeAdapter<T> adapter;

    MyGsonResponseBodyConverter(Gson gson, TypeAdapter<T> adapter) {
        this.gson = gson;
        this.adapter = adapter;
    }

    public Object convert(ResponseBody value) throws IOException {
        JsonReader jsonReader = gson.newJsonReader(value.charStream());
        final Object obj = adapter.read(jsonReader);
//        try {
//            if (obj instanceof BaseResponse) {//token过期则抛出异常
//                BaseResponse baseResponse = (BaseResponse) obj;
//                String systemCode = baseResponse.getSystemCode();
//                String systemMsg = baseResponse.getSystemMsg();
//                if (systemMsg.contains("会话")) {
//                    Log.e("jiejing", systemMsg + "    sessionId = " + CMemoryData.getSessionId());
//                    throw new TokenInvalidException();//session失效
//                } else if(systemCode.equals(ErrorConstant.E_107)){
//                    throw new ApiException(systemCode, systemMsg);
//                }
//                    Result result = baseResponse.getResult();
//                    if (result != null) {
//                        Error error = result.getError();
//                        if (error != null) {
//                            String businessCode = error.getBusinessCode();
//                            String businessMsg = error.getBusinessMsg();
//                            if (businessCode != null) {
//                                /**
//                                 * session相关的操作，需要底层拦截
//                                 * 其余错误抛出ApiException，然后放行
//                                 * 在实现ObserverOnNextListener的onError里可以拿到错误信息
//                                 */
//                                if (businessMsg.contains("会话")) {
//                                    throw new TokenInvalidException();//session失效
//                                } else {
//                                    throw new ApiException(businessCode, businessMsg);
//                                }
//                            }
//                        }
//                        return obj;
//                    }
//            }
//        }finally {
//            value.close();
//        }
        return obj;
    }
}
